package com.industrial.white.service.impl;

import com.alibaba.csp.sentinel.util.IdUtil;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.white.domain.WhiteNotice;
import com.industrial.white.mapper.WhiteNoticeMapper;
import com.industrial.white.service.IWhiteNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WhiteWhiteNoticeServiceImpl implements IWhiteNoticeService {

    @Autowired
    private WhiteNoticeMapper whiteNoticeMapper;

    @Override
    public List<WhiteNotice> selectNoticeList(WhiteNotice notice) {

        return whiteNoticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(WhiteNotice notice) {
        notice.setNoticeId(IdUtils.simpleUUID());
        notice.setCreateTime(new Date());
        return whiteNoticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(WhiteNotice notice) {

        WhiteNotice whiteNotice = whiteNoticeMapper.selectNoticeById(notice.getNoticeId());
        String userId = SecurityUtils.getUserId();
        if(!StringUtils.equals(whiteNotice.getCreateBy(),userId)){
            throw new RuntimeException("您无权限修改他人新增的通知公告！");
        }


        notice.setUpdateTime(new Date());
        return whiteNoticeMapper.updateNotice(notice);
    }

    @Override
    public int deleteNoticeByIds(String noticeId) {
        WhiteNotice whiteNotice = whiteNoticeMapper.selectNoticeById(noticeId);
        String userId = SecurityUtils.getUserId();
        if(!StringUtils.equals(whiteNotice.getCreateBy(),userId)){
            throw new RuntimeException("您无权限删除他人新增的通知公告！");
        }
        String[] noticeIds = new String[]{noticeId};
        return whiteNoticeMapper.deleteNoticeByIds(noticeIds);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public WhiteNotice selectNoticeById(String noticeId) {
        return whiteNoticeMapper.selectNoticeById(noticeId);
    }
}
